package game

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
	gameReq "github.com/flipped-aurora/gin-vue-admin/server/model/game/request"
	gameRes "github.com/flipped-aurora/gin-vue-admin/server/model/game/response"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BsUrlApi struct{}

// CreateUrl
// @Tags      BsUrl
// @Summary   创建文件存储
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsUrl                  true  "api路径, api中文描述, api组, 方法"
// @Success   200   {object}  response.Response{msg=string}  "创建文件存储"
// @Router    /api/createUrl [post]
func (s *BsUrlApi) CreateUrl(c *gin.Context) {
	var param game.BsUrl
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = urlService.CreateUrl(param)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteUrl
// @Tags      BsUrl
// @Summary   删除静态资源路径
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsUrl                  true  "Id"
// @Success   200   {object}  response.Response{msg=string}  "删除静态资源路径"
// @Router    /api/deleteUrl [delete]
func (s *BsUrlApi) DeleteUrl(c *gin.Context) {
	var param game.BsUrl
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = urlService.DeleteUrl(param)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// GetUrlList
// @Tags      BsUrl
// @Summary   分页获取静态资源路径列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      gameReq.UrlParams                               true  "分页获取静态资源路径列表"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取静态资源路径列表,返回包括列表,总数,页码,每页数量"
// @Router    /api/getUrlList [get]
func (s *BsUrlApi) GetUrlList(c *gin.Context) {
	var pageInfo gameReq.UrlParams
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := urlService.GetUrlList(pageInfo.BsUrl, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetUrlById
// @Tags      BsUrl
// @Summary   根据id获取静态资源路由
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.GetById                                   true  "根据id获取api"
// @Success   200   {object}  response.Response{data=gameRes.BsUrlResponse}  "根据id获取静态资源路由,返回包括静态资源路由详情"
// @Router    /api/getUrlById [get]
func (s *BsUrlApi) GetUrlById(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	url, err := urlService.GetUrlById(idInfo.Id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(gameRes.BsUrlResponse{Url: url}, "获取成功", c)
}

// UpdateUrl
// @Tags      BsUrl
// @Summary   修改静态资源路由
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsUrl                  true  "静态资源路由路径"
// @Success   200   {object}  response.Response{msg=string}  "修改静态资源路由"
// @Router    /api/updateUrl [put]
func (s *BsUrlApi) UpdateUrl(c *gin.Context) {
	var url game.BsUrl
	err := c.ShouldBindJSON(&url)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(url, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = urlService.UpdateUrl(url)
	if err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
		return
	}
	response.OkWithMessage("修改成功", c)
}
